package com.ruoyi.web.controller.net;

import com.ruoyi.net.domain.ElementOspfv2;
import com.ruoyi.net.domain.ElementOspfv2Domain;
import com.ruoyi.net.domain.NetElement;
import com.ruoyi.net.netconf.util.DeviceManager;
import com.ruoyi.net.service.IElementOspfv2Service;
import com.ruoyi.net.service.INetElementService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.net.domain.ElementOspfv2Route;
import com.ruoyi.net.service.IElementOspfv2RouteService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 网元-OSPFV2进程配置-路由重分发Controller
 * 
 * @author net
 * @date 2024-11-17
 */
@Api(tags = "网元-OSPFV2进程配置-路由重分发")
@RestController
@RequestMapping("/element/ospfv2/route")
public class ElementOspfv2RouteController extends BaseController {
    @Autowired
    private IElementOspfv2RouteService elementOspfv2RouteService;

    @Autowired
    private IElementOspfv2Service elementOspfv2Service;

    @Autowired
    private INetElementService netElementService;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private DeviceManager deviceManager;

    /**
     * 查询网元-OSPFV2进程配置-路由重分发列表
     */
    @ApiOperation(value = "查询网元-OSPFV2进程配置-路由重分发列表", httpMethod = "GET")
    //    @PreAuthorize("@ss.hasPermi('system:route:list')")
    @GetMapping("/list")
    public TableDataInfo list(ElementOspfv2Route elementOspfv2Route) {
        startPage();
        List<ElementOspfv2Route> list = elementOspfv2RouteService.selectElementOspfv2RouteList(elementOspfv2Route);
        return getDataTable(list);
    }

    /**
     * 导出网元-OSPFV2进程配置-路由重分发列表
     */
    @ApiOperation(value = "导出网元-OSPFV2进程配置-路由重分发列表", httpMethod = "POST")
    //    @PreAuthorize("@ss.hasPermi('system:route:export')")
    @Log(title = "网元-OSPFV2进程配置-路由重分发", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ElementOspfv2Route elementOspfv2Route) {
        List<ElementOspfv2Route> list = elementOspfv2RouteService.selectElementOspfv2RouteList(elementOspfv2Route);
        ExcelUtil<ElementOspfv2Route> util = new ExcelUtil<ElementOspfv2Route>(ElementOspfv2Route.class);
        util.exportExcel(response, list, "网元-OSPFV2进程配置-路由重分发数据");
    }

    /**
     * 获取网元-OSPFV2进程配置-路由重分发详细信息
     */
    @ApiOperation(value = "获取网元-OSPFV2进程配置-路由重分发详细信息", httpMethod = "GET")
    //    @PreAuthorize("@ss.hasPermi('system:route:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(elementOspfv2RouteService.selectElementOspfv2RouteById(id));
    }

    /**
     * 新增网元-OSPFV2进程配置-路由重分发
     */
    @ApiOperation(value = "新增网元-OSPFV2进程配置-路由重分发", httpMethod = "POST")
    //    @PreAuthorize("@ss.hasPermi('system:route:add')")
    @Log(title = "网元-OSPFV2进程配置-路由重分发", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ElementOspfv2Route elementOspfv2Route) {
        sendXml(elementOspfv2Route);
        return toAjax(elementOspfv2RouteService.insertElementOspfv2Route(elementOspfv2Route));
    }

    /**
     * 修改网元-OSPFV2进程配置-路由重分发
     */
    @ApiOperation(value = "修改网元-OSPFV2进程配置-路由重分发", httpMethod = "PUT")
    //    @PreAuthorize("@ss.hasPermi('system:route:edit')")
    @Log(title = "网元-OSPFV2进程配置-路由重分发", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ElementOspfv2Route elementOspfv2Route) {
        sendXml(elementOspfv2Route);
        return toAjax(elementOspfv2RouteService.updateElementOspfv2Route(elementOspfv2Route));
    }

    /**
     * 删除网元-OSPFV2进程配置-路由重分发
     */
    @ApiOperation(value = "删除网元-OSPFV2进程配置-路由重分发", httpMethod = "DELETE")
    //    @PreAuthorize("@ss.hasPermi('system:route:remove')")
    @Log(title = "网元-OSPFV2进程配置-路由重分发", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(elementOspfv2RouteService.deleteElementOspfv2RouteByIds(ids));
    }

    private void sendXml(ElementOspfv2Route entity) {
        // TODO: 2024/12/3  设备联调，添加并激活
        if (sysConfigService.selectDebugEnabled()) {
            NetElement netElement = netElementService.getCacheObject(entity.getElementId());
            deviceManager.getDevice(netElement.getIp());

            ElementOspfv2 elementOspfv2 = elementOspfv2Service.selectElementOspfv2ById(entity.getOspfv2Id());

            String operation = "create";
            if ("0".equals(entity.getStatus())) {
                operation = "remove";
            }
            StringBuilder xml = new StringBuilder();
            xml.append("<ospf xmlns=\"http://fiberhome.com/fhnw/yang/ipprotocol/routing/fos-ospf\">" +
                    "    <ospf-processes>" +
                    "      <ospf-process nc:operation=\"" + operation + "\">" +
                    "        <process-id>" + elementOspfv2.getOspfId() + "</process-id>" +
                    // ------ start 路由重分发 ------
                    "        <ospf-redistribute>" +
                    "          <protocol>" + entity.getRouteProtocol() + "</protocol>" +
                    "          <ospf-process-id>" + entity.getProtocolDemo() + "</ospf-process-id>" +
                    "          <isis-process-name>" + entity.getProtocolName() + "</isis-process-name>");
            if ("enable".equalsIgnoreCase(entity.getStrategyEnable()) || "1".equals(entity.getStrategyEnable())) {
                xml.append("          <route-policy-name>" + entity.getMappingName() + "</route-policy-name>");
            }
            if ("enable".equalsIgnoreCase(entity.getMeasureEnable()) || "1".equals(entity.getMeasureEnable())) {
                xml.append("          <type>" + entity.getMeasureType() + "</type>" +
                        "          <cost>" + entity.getMeasureValue() + "</cost>");
            }

            if ("enable".equalsIgnoreCase(entity.getMarkEnable()) || "1".equals(entity.getMarkEnable())) {
                xml.append("          <tag>" + entity.getMarkValue() + "</tag>");
            }
            xml.append("        </ospf-redistribute>" +
                    // ------ end 路由重分发 ------
                    "      </ospf-process>" +
                    "    </ospf-processes>" +
                    "  </ospf>");
            System.out.println(xml.toString());
            deviceManager.editConfig(xml.toString(), "merge");
        }
    }
}